﻿Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework

Public Class MyModel
    Implements ICanDrawItself

    Private mModel As Model
    Private mPolygonCount As Integer
    Private mMinimumDistance As Single = 0
    Private mModelBoundingSphere As BoundingSphere
    Private mTexture As Texture2D
    ' Array holding all the bone transform matrices for the entire model.
    Private mBoneTransforms As Matrix()

    Public Sub New(ByVal Model As Model)
        Me.Model = Model
    End Sub

    Public Sub New()

    End Sub

    Public Property Model() As Model
        Get
            Model = mModel
        End Get
        Set(ByVal value As Model)
            mModel = value

            ' Allocate the transform matrix array.
            mBoneTransforms = New Matrix(mModel.Bones.Count) {}
        End Set
    End Property

    Public Property Texture() As Texture2D
        Get
            Texture = mTexture
        End Get
        Set(ByVal value As Texture2D)
            mTexture = value
        End Set
    End Property

    Public Property PolygonCount() As Integer
        Get
            PolygonCount = mPolygonCount
        End Get
        Set(ByVal value As Integer)
            mPolygonCount = value
        End Set
    End Property

    Public Property MinimumDistance() As Single Implements ICanDrawItself.MinimumDistance
        Get
            MinimumDistance = mMinimumDistance
        End Get
        Set(ByVal value As Single)
            mMinimumDistance = value
        End Set
    End Property

    Public ReadOnly Property BoundingSphere() As BoundingSphere Implements ICanDrawItself.BoundingSphere
        Get
            Dim i As Integer = 0
            For Each mm As ModelMesh In Model.Meshes
                If i = 0 Then
                    BoundingSphere = mm.BoundingSphere
                Else
                    BoundingSphere.CreateMerged(BoundingSphere, mm.BoundingSphere)
                End If
                i += 1
            Next
        End Get
    End Property

    ''' <summary>
    ''' Draws the 3D specified model.
    ''' </summary>
    ''' <param name="camera">The Camera3D that the model should get its View matrix from.</param>
    ''' <param name="world">Transformation matrix for world coords.</param>
    Public Sub Draw(ByVal camera As Camera3D, ByVal world As Matrix) Implements ICanDrawItself.Draw

        ' Set the world matrix as the root transform of the model.
        Model.Root.Transform = world

        With Model
            ' Look up combined bone matrices for the entire model.
            .CopyAbsoluteBoneTransformsTo(mBoneTransforms)

            For Each m As ModelMesh In .Meshes
                For Each e As BasicEffect In m.Effects
                    e.World = mBoneTransforms(m.ParentBone.Index)
                    e.View = camera.View
                    e.Projection = camera.Projection
                    If mTexture IsNot Nothing Then
                        e.Texture = mTexture
                        e.TextureEnabled = True
                    End If
                    e.EnableDefaultLighting()
                Next
                m.Draw()
            Next
        End With
    End Sub

End Class